const api = require("../../server/Api.js");
const util = require("../../utils/util.js");
const app = getApp();

//订单详情 状态 99-全部 0-已取消 1-待付款 2-待发货 3-待收货 4-已关闭 5-已完成 88-待评价
Page({
  /**
   * 页面的初始数据
   */
  data: {
    curPage: 1,
    pageSize: 6,
    hasMore: true,
    items: [], //总商品数
    curOrderId: {}, //按钮操作的当前id
    curOrderTypeName: "",
    emptyType: 2
  },

  onLoad: function(options) {
    this.refreshData();
  },

  onShow: function() {

  },
  //下拉啊刷新
  onPullDownRefresh: function() {
    this.refreshData();
  },

  //刷新列表
  refreshData: function() {
    let userInfo = app.globalData.userBean;
    if (!(userInfo && userInfo.consumerId)) {
      tt.switchTab({
        url: '../main-person-new/main-person',
        success() {
          tt.navigateTo({
            url: '../login/login',
          })
        }
      })
      return
    }
    tt.showNavigationBarLoading();
    //app.showLoading();
    var that = this;
    this.setData({
      hasMore: true,
    })
    this.queryOrderList(1, this.data.pageSize);
  },

  //上拉加载
  onReachBottom: function() {
    if (this.data.hasMore == false) return;
    tt.showNavigationBarLoading();
    let page = this.data.curPage + 1;
    this.queryOrderList(page, this.data.pageSize);
  },

  //分页查询订单
  queryOrderList: function(page, pageSize) {
    let that = this;
    let userBean = app.globalData.userBean;
    api.listRefundOrder(userBean.consumerId, pageSize, page).then(res => {
      //请求成功
      that.querySuccess(res, page)
      tt.stopPullDownRefresh();
      that.setData({
        emptyType: 2
      })
    }).catch(msg => {
      tt.stopPullDownRefresh();
      that.setData({
        emptyType: 101
      })
      var message = typeof msg == "string" ? msg : msg.message;
      app.showToast(message);
    })
  },

  //查询成功
  querySuccess: function(res, curPage) {
    let that = this;
    let records = res.records;
    let maxPage = parseInt(res.pages);
    var datas = curPage == 1 ? [] : this.data.items;
    if (records) {
      datas = datas.concat(records);
    }
    that.setData({
      hasMore: maxPage > curPage,
      items: datas,
      curPage: curPage,
    })
    console.log(this.data)
  },

  // 按钮操作
  opertionTap: function(e) {
    let orderId = e.currentTarget.dataset.id;
    let orderType = e.currentTarget.dataset.type;
    let btnName = e.currentTarget.id;
    this.setData({
      curOrderId: orderId,
      curOrderTypeName: orderType == "1" ? "退款" : "退货"
    })
    switch (btnName) {
      case "取消退款":
        this.showCancelDialog()
        break;
      case "取消退货":
        this.showCancelDialog()
        break;
      case "修改申请":
        tt.navigateTo({
          url: '../after-sales-choose/after-sales-choose?sid=' + orderId,
        })
        break;
      case "提醒打款":
        app.showToast("暂不支持此功能")
        break;
    }
  },
  showCancelDialog: function() {
    this.setData({
      showCancelDialog: true
    })
  },
  hiddenDialog() {
    this.setData({
      showCancelDialog: false
    })
  },
  onConfirm(orderId) {
    app.showLoading()
    let params = {
      "refundId": this.data.curOrderId
    }
    api.refundCancel(params).then(res => {
      tt.hideLoading()
      this.refreshData();
    }).catch(msg => {
      tt.hideLoading()
      let message = typeof msg == "string" ? msg : msg.message;
      app.showToast(message);
    })
  },
  toDetail(e) {
    tt.navigateTo({
      url: '../after-sale-info/index?id=' + e.currentTarget.dataset.id,
    })
  }
})